const BASE_URL='http://10.155.218.3:3000'
import store from '@/store/index.js'

export const Request =(option)=>{
	
	// 判断是否传入header头的token进行用户是否登录
	 option.header=option.header || {}
	if(option.header.token){
		option.header.token=store.state.user.token	
	
		if(!option.header.token){
			uni.showToast({
				title:'请先登录',
				icon:"none"
			})
			return uni.navigateTo({
				url:"/pages/login/login"
			})
		}
	}
	
	return new Promise ((resolve,reject)=>{	
		uni.request({
			url:BASE_URL+option.url,
			method:option.method || 'GET',
			data:option.data || {},
			header:option.header || {
			"Content-Type":"application/json",
			"Content-Type":"application/x-www-form-urlencoded"
		},
			success:(res)=>{
				if(res.statusCode!=200){
					return uni.showToast({
						title:'获取数据失败'
					})
				}
				resolve(res)
			},
			fail:(err)=> {
				uni.showToast({
					title:'请求接口失败'
				})
				reject(err)
			}
		})
	})
}